package cn.piaoyi.web.taglib;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.io.Writer;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.components.Component;

import cn.piaoyi.service.web.Pages;

import com.opensymphony.xwork2.util.ValueStack;

public class Page extends Component {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(Page.class);

	public Page(ValueStack arg0) {
		super(arg0);
	}

	protected String argPage = "page";

	protected String argTotal = "total";

	protected int pageSep = 7;

	private String value;

	public String getArgPage() {
		return argPage;
	}

	public void setArgPage(String argPage) {
		this.argPage = argPage;
	}

	public String getArgTotal() {
		return argTotal;
	}

	public void setArgTotal(String argTotal) {
		this.argTotal = argTotal;
	}

	public int getPageSep() {
		return pageSep;
	}

	public void setPageSep(int pageSep) {
		this.pageSep = pageSep;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public boolean start(Writer writer) {
		boolean result = super.start(writer);

		if (value == null) {
			value = "top";
		} else if (altSyntax()) {
			if (value.startsWith("%{") && value.endsWith("}")) {
				value = value.substring(2, value.length() - 1);
			}
		}

		Pages pages = (Pages) this.getStack().findValue(value);

		// 一行显示页数
		int pagesep = this.pageSep;
		// 总行数
		int allpagesep = (int) Math.ceil((pages.getAllPage() + pagesep - 1)
				/ pagesep);
		// 当前行数
		int cpagesep = (int) Math.ceil((pages.getCpage() + pagesep - 1)
				/ pagesep);
		// 当前一行显示页数
		int cnum;
		if (pages.getAllPage() > 0) {
			// 在页中
			if (cpagesep != allpagesep) {
				cnum = pagesep;
			}
			// 页末
			else {
				cnum = pages.getAllPage() % pagesep;
				// 正好整除
				if (cnum == 0) {
					cnum = pagesep;
				}
			}
		} else {
			cnum = 0;
		}
		// System.out.println("cpage:" + pages.getCpage() + " cpagesep:" +
		// cpagesep + " allpagesep:" + allpagesep);

		String fileName = pages.getFileName();
		StringBuffer sb = new StringBuffer();
		fileName = getURL(fileName);

		/**
		 * previous butten ,append to <a href='xxx.lt?page=x&t=x'><img></a>
		 */
		//sb.append("<div class=\"show_page\">");
		//共 80640 条
		sb.append("<li><a>共 ");
		sb.append(pages.getTotalNum());
		sb.append(" 条</a></li>");
		 //上页
        if(pages.getCpage() == 1){
        	sb.append("<li><a href=\"");
            sb.append(fileName);
            sb.append("pagenum=");
            sb.append(pages.getPerPageNum());
            sb.append("&");
            sb.append(this.getArgPage());
            sb.append("=1&");
            sb.append(this.getArgTotal());
            sb.append("=");
            sb.append(pages.getTotalNum());
            sb.append("\">");
            sb.append("上一页");
            sb.append("</a></li>");
        }else{
        	sb.append("<li><a href=\"");
            sb.append(fileName);
            sb.append("pagenum=");
            sb.append(pages.getPerPageNum());
            sb.append("&");
            sb.append(this.getArgPage());
            sb.append("=");
            sb.append(pages.getCpage()-1);
            sb.append("&");
            sb.append(this.getArgTotal());
            sb.append("=");
            sb.append(pages.getTotalNum());
            sb.append("\">");
            sb.append("上一页");
            sb.append("</a></li>");//
        }

		/**
		 * middle butten ,append to <a href='xxx.lt?page=x&t=x'><img></a>
		 */
		for (int i = 0; i < cnum; i++) {
			sb.append("<li><a ");
			if (pages.getCpage() == (i + 1) + ((cpagesep - 1) * pagesep)) {
				sb.append("style=\"color:red\" ");
			}
			sb.append("href=\"");
			sb.append(fileName);
			sb.append("&");
			sb.append(this.getArgPage());
			sb.append("=");
			sb.append(((i + 1) + ((cpagesep - 1) * pagesep)));
			sb.append("&");
			sb.append(this.argTotal);
			sb.append("=");
			sb.append(pages.getTotalNum());
	        sb.append("\">");
			sb.append((i + 1) + ((cpagesep - 1) * pagesep));
			sb.append("</a></li>");
		}
		/**
		 * next butten ,append to <a href='xxx.lt?page=x&t=x'><img></a>
		 */

		if (cpagesep < allpagesep) {
			sb.append("<li><a href=\"");
			sb.append(fileName);
			sb.append("&");
			sb.append(this.getArgPage());
			sb.append("=");
			sb.append(cpagesep * pagesep+1);
			sb.append("&");
			sb.append(this.argTotal);
			sb.append("=");
			sb.append(pages.getTotalNum());
			sb.append("\">");
			sb.append("...");
			sb.append("</a></li>");
			
			if(cpagesep * pagesep != pages.getAllPage()-1){
				sb.append("<li><a href=\"");
	            sb.append(fileName);
	            sb.append("pagenum=");
	            sb.append(pages.getPerPageNum());
	            sb.append("&");
	            sb.append(this.getArgPage());
	            sb.append("=");
	            sb.append(pages.getAllPage()-1);
	            sb.append("&");
	            sb.append(this.getArgTotal());
	            sb.append("=");
	            sb.append(pages.getTotalNum());
	            sb.append("\">");
	            sb.append(pages.getAllPage()-1);
	            sb.append("</a></li>");
			}
			
			sb.append("<li><a href=\"");
            sb.append(fileName);
            sb.append("pagenum=");
            sb.append(pages.getPerPageNum());
            sb.append("&");
            sb.append(this.getArgPage());
            sb.append("=");
            sb.append(pages.getAllPage());
            sb.append("&");
            sb.append(this.getArgTotal());
            sb.append("=");
            sb.append(pages.getTotalNum());
            sb.append("\">");
            sb.append(pages.getAllPage());
            sb.append("</a></li>");
		}
		
		//下页
        if(pages.getCpage() >=pages.getAllPage()){
        	sb.append("<li><a href=\"");
            sb.append(fileName);
            sb.append("pagenum=");
            sb.append(pages.getPerPageNum());
            sb.append("&");
            sb.append(this.getArgPage());
            sb.append("=");
            sb.append(pages.getAllPage());
            sb.append("&");
            sb.append(this.getArgTotal());
            sb.append("=");
            sb.append(pages.getTotalNum());
            sb.append("\">");
            sb.append("下一页");
            sb.append("</a></li>");
        }else{
        	sb.append("<li><a href=\"");
            sb.append(fileName);
            sb.append("pagenum=");
            sb.append(pages.getPerPageNum());
            sb.append("&");
            sb.append(this.getArgPage());
            sb.append("=");
            sb.append(pages.getCpage()+1);
            sb.append("&");
            sb.append(this.getArgTotal());
            sb.append("=");
            sb.append(pages.getTotalNum());
            sb.append("\">");
            sb.append("下一页");
            sb.append("</a></li>");//
        }
       // sb.append("</div>");
		try {
			writer.write(sb.toString());
		} catch (IOException e) {
			logger.error(e);
		}

		return result;
	}

	private String getURL(String fileName){
		if (fileName.indexOf("?") == -1) {
	        fileName = fileName + "?";
	      }else {
	        if (!fileName.endsWith("&")) {
	          fileName = fileName + "&";
	        }
	     }
		return fileName;
	}

}
